﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conference.Logic.Base;

namespace Conference.Logic.Schedule
{
    /// <summary>
    /// Abstract class that represent a session (moorning or afternoon)
    /// </summary>
    public abstract class Session
    {
        #region Attributes
        protected List<ItemSession> sessions;
        #endregion

        #region Constructor
        /// <summary>
        /// Instantiate a new base class session and create a new list of session item
        /// </summary>
        public Session()
        {
            try
            {
                if (sessions == null)
                    sessions = new List<ItemSession>();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add a new item session to the list
        /// </summary>
        /// <param name="item">ItemSession object</param>
        /// <returns>Tru or false</returns>
        public virtual bool Add(ItemSession item)
        {
            try
            {
                this.sessions.Add(item);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Create a new item session object and add to the list
        /// </summary>
        /// <param name="description">Title</param>
        /// <param name="duration">Length duration</param>
        /// <param name="startTime">Start time</param>
        /// <returns>True or false</returns>
        public virtual bool Add(string description, TimeSpan duration, TimeSpan startTime)
        {
            try
            {
                ItemSession item = new ItemSession(description, duration, startTime);
                item.Type = ItemType.Default;
                this.sessions.Add(item);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Create a new item session object according with type and add to the list.
        /// Used when type equals lightning.
        /// </summary>
        /// <param name="description">Title</param>
        /// <param name="type">Type of item (default, ligthning)</param>
        /// <param name="startTime">Start time</param>
        /// <returns>True or false</returns>
        public virtual bool Add(string description, ItemType type, TimeSpan startTime)
        {
            try
            {
                ItemSession item = new ItemSession(description, type, startTime);
                this.sessions.Add(item);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get the time for next item session. Check start time from last item and sum with its duration.
        /// </summary>
        /// <returns>A timespan that represents when next session will start</returns>
        public TimeSpan GetNextSession()
        {
            if (sessions.Count > 0)
            {
                return (sessions.Select(a => a.StartTime.Add(a.Duration)).Last());
            }
            else
            {
                if (this is Moorning)
                    return Parameter.Default.StartMorning;
                else if (this is Afternoon)
                    return Parameter.Default.StartAfternoon;
                else
                    throw new InvalidCastException("Invalid session!");
            }
        }
        #endregion
    }
}
